
import bi_common as bi
import bi_standalone as bis
import vehicle_sample as vs
import env_sample as es
import datetime as dt

# 全局参数初始化
turn_curvature = 0.05  # 1/m
straight_curvature = 0.01  # 1/m
speed_down_gap = 5.0  # KPH


# Start脚本
class RisingDetector:
    def __init__(self, rise_thresh, fall_thresh, greater_rule):
        self.state = False
        self.rise_thresh = rise_thresh
        self.fall_thresh = fall_thresh
        self.greater_rule = greater_rule

    def update(self, value):
        if self.state:
            if self.greater_rule:
                new_state = value >= self.fall_thresh
            else:
                new_state = value <= self.fall_thresh
        else:
            if self.greater_rule:
                new_state = value >= self.rise_thresh
            else:
                new_state = value <= self.rise_thresh
        rising = self.state is False and new_state is True
        self.state = new_state
        return rising


turn_left_detector = RisingDetector(float(turn_curvature), float(straight_curvature), True)
turn_right_detector = RisingDetector(-float(turn_curvature), -float(straight_curvature), False)
over_speed_detector = RisingDetector(1000.0, 1000.0, True)


# Loop脚本
def loop():
    # 获取样本
    v_sample = vs.get_vehicle_sample()
    e_sample = es.get_env_sample()

    # 时间更新
    date_time = bi.session + dt.timedelta(seconds=bi.time)

    # 统计总行驶时间
    bi.Util.set_to_value_report('total-hr-age', bi.time / 3600)

    # 统计总行驶里程
    if v_sample is not None:
        bi.Util.set_to_value_report('total-km-age', v_sample.kilometer_age)

    # 统计各时间段的行驶时间
    bi.Util.add_to_numeric_histline_report('time-hr-age', date_time.hour, bi.interval / 3600)

    # 统计各时间段的行驶里程
    if v_sample is not None:
        bi.Util.add_to_numeric_histline_report('time-km-age', date_time.hour, v_sample.speed / 3.6 * bi.interval * 0.001)

    # 统计不同车速下的行驶时间
    if v_sample is not None:
        bi.Util.add_to_numeric_histline_report('speed-hr-age', v_sample.speed, bi.interval / 3600)

    # 统计不同车速下的行驶里程
    if v_sample is not None:
        bi.Util.add_to_numeric_histline_report('speed-km-age', v_sample.speed, v_sample.speed / 3.6 * bi.interval * 0.001)

    # 统计不同天气下的行驶时间
    if v_sample is not None and e_sample is not None:
        bi.Util.add_to_label_histline_report('weather-hr-age', e_sample.weather_type, bi.interval / 3600)

    # 统计不同天气下的行驶里程
    if v_sample is not None and e_sample is not None:
        bi.Util.add_to_label_histline_report('weather-km-age', e_sample.weather_type, v_sample.speed / 3.6 * bi.interval * 0.001)

    # 统计不同道路类型下的行驶时间
    if v_sample is not None and e_sample is not None:
        bi.Util.add_to_label_histline_report('road-hr-age', e_sample.road_type, bi.interval / 3600)

    # 统计不同道路类型下的行驶里程
    if v_sample is not None and e_sample is not None:
        bi.Util.add_to_label_histline_report('road-km-age', e_sample.road_type, v_sample.speed / 3.6 * bi.interval * 0.001)

    # 统计左转次数
    if v_sample is not None and v_sample.curvature_valid and turn_left_detector.update(v_sample.curvature):
        bi.Util.add_to_value_report('turn-left', 1)

    # 统计右转次数
    if v_sample is not None and v_sample.curvature_valid and turn_right_detector.update(v_sample.curvature):
        bi.Util.add_to_value_report('turn-right', 1)

    # 统计超速次数
    if v_sample is not None and e_sample is not None and e_sample.speed_limit_valid:
        over_speed_detector.rise_thresh = e_sample.speed_limit
        over_speed_detector.fall_thresh = e_sample.speed_limit - float(speed_down_gap)
        if over_speed_detector.update(v_sample.speed):
            bi.Util.add_to_value_report('over-speed', 1)


bis.run_standalone('input', loop)
